// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   SAPConn.java

package com.nondo.dean.saprfc.common;

import com.sap.conn.jco.*;
import com.sap.conn.jco.ext.DestinationDataProvider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

/**
 * sap连接类
 */
public class SapUtil extends BaseBean {
	private static final Logger logger = LoggerFactory.getLogger(SapUtil.class);
	private static final Map<String, JCoDestination> map = new HashMap<String, JCoDestination>();
	private static final String conn="SAPConn800";
	private SapUtil() {
	}
	public static JCoDestination getDestination(String propName) {
		JCoDestination destination = null;
		propName=(propName==null||"".equals(propName)) ? conn : propName;
		try {
			if (map.containsKey(propName)) {
				destination = map.get(propName);
			} else {
				String fileName = propName + ".properties";
				Properties prop = new Properties();
				prop.setProperty(DestinationDataProvider.JCO_ASHOST,getPropValue(fileName, "HostName"));
				prop.setProperty(DestinationDataProvider.JCO_SYSNR,getPropValue(fileName, "SystemNumber"));
				prop.setProperty(DestinationDataProvider.JCO_CLIENT,getPropValue(fileName, "SAPClient"));
				prop.setProperty(DestinationDataProvider.JCO_USER,getPropValue(fileName, "Userid"));
				prop.setProperty(DestinationDataProvider.JCO_PASSWD,getPropValue(fileName, "Password"));
				prop.setProperty(DestinationDataProvider.JCO_LANG, "ZH");
				prop.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");
				prop.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "100");
				createDataFile(propName, "jcoDestination", prop);
				destination = JCoDestinationManager.getDestination(propName);
				map.put(propName, destination);
				logger.info("getDestination-map:"+map);
				logger.info("getDestination:"+destination);
			}
		} catch (JCoException e) {
			logger.error("getDestination:"+e.getMessage(), e);
		}
		return destination;
	}

	public static JCoFunction getFunction(String propName, String functionName) {
		JCoFunction function = null;
		try {
		    logger.info("getDestination(propName).getRepository():"+getDestination(propName).getRepository());
			function = getDestination(propName).getRepository().getFunctionTemplate(functionName).getFunction();
			logger.info("getDestination(propName).getRepository().getFunctionTemplate(functionName):"+getDestination(propName).getRepository().getFunctionTemplate(functionName));
		} catch (Exception e) {
			logger.error("getFunction:"+e.getMessage(), e);
		}
		return function;
	}

	public static JCoFunction executeFunction(String propName,String functionName){
		return executeFunction(propName, functionName, null);
	}

	public static JCoFunction executeFunction(String propName,String functionName,Map<String, String> impMap){
		JCoFunction function = null;
		try {
			function = getDestination(propName).getRepository().getFunctionTemplate(functionName).getFunction();
			JCoParameterList impParam = function.getImportParameterList();
			if(impMap!=null&&impMap.size()>0){
				for (Entry<String, String> entry:impMap.entrySet()) {
					impParam.setValue(entry.getKey(),entry.getValue());
				} 
			}
			function.execute(getDestination(propName));
		} catch (Exception e) {
			logger.error("executeFunction:"+e.getMessage(), e);
		}
		return function;
	}
	public static JCoFunction executeFunction(String propName,JCoFunction function){
		try {
		    function.execute(getDestination(propName));
			logger.info("executeFunction:"+function);
		} catch (Exception e) {
			logger.error("executeFunction:"+e.getMessage(), e);
		}
		return function;
	}

	/**
	 * sap返回消息表转换成string字符串
	 * @param LT_RETURN
	 * @return
	 */
	public static String writeSapMessage(JCoTable LT_RETURN){
		Map<String, String> typeMap=new HashMap<String, String>();
		typeMap.put("S", "S:成功");
		typeMap.put("E", "E:错误");
		typeMap.put("W", "W:警告");
		typeMap.put("I", "I:信息");
		typeMap.put("A", "A:中断");
		String re_str="";
		for (int i = 0; i < LT_RETURN.getNumRows(); i++) {
			String v_TYPE=LT_RETURN.getString("TYPE");//消息类型
			String v_NUMBER=LT_RETURN.getString("NUMBER");//消息编号
			String v_MESSAGE=LT_RETURN.getString("MESSAGE");//消息文本
			String v_TMP_STR_1=LT_RETURN.getString("MESSAGE_V1");
			String v_TMP_STR_2=LT_RETURN.getString("MESSAGE_V2");
			String v_TMP_STR_3=LT_RETURN.getString("MESSAGE_V3");
			String v_TMP_STR_4=LT_RETURN.getString("MESSAGE_V4");
			String v_ROW=LT_RETURN.getString("ROW");
			String v_FIELD=LT_RETURN.getString("FIELD");
			v_TYPE=Util.null2String(typeMap.get(v_TYPE));
			v_MESSAGE=v_MESSAGE+" 详细:"+v_TMP_STR_1+"  "+v_TMP_STR_2+"  "+v_TMP_STR_3+"  "+v_TMP_STR_4+"  "+v_ROW+"  "+v_FIELD;
			if(v_TYPE.startsWith("E")||v_TYPE.startsWith("W")||v_TYPE.startsWith("A")){
				re_str+="类型:"+v_TYPE+" 编号:"+v_NUMBER+" 文本:"+v_MESSAGE;
			}else{
				re_str+="类型:"+v_TYPE+" 编号:"+v_NUMBER+" 文本:"+v_MESSAGE;
			}
			LT_RETURN.nextRow();
		}
		return re_str;
	}

	/**
	 * 创建sap的jcoDestination连接池文件
	 * @param name
	 * @param suffix
	 * @param properties
	 */
	private static void createDataFile(String name, String suffix, Properties properties) {
		File cfg = new File(name + "." + suffix);
		if (!cfg.exists()) {
			try {
				FileOutputStream outputFile = new FileOutputStream(cfg, false);
				properties.store(outputFile, null);
				outputFile.close();
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
		}
	}
}
